/********************************************************************************************************************************************************************************
 * 
 * Class Name  	: 
 * Version 	  	: 
 * Description 	: 
 * 
 ********************************************************************************************************************************************************************************
 * 
 * Author 		: Karim Beyrouti
 * Date 			: 
 * 
 ********************************************************************************************************************************************************************************
 * 
 * METHODS
 * 
 *
 * PROPERTIES
 * 
 *
 * EVENTS
 * 
 * 
 ********************************************************************************************************************************************************************************
 * 				:
 *
 *
 *********************************************************************************************************************************************************************************
 * NOTES			:
 **********************************************************************************************************************************************************************************/

package com.kurst.air.utils{
	
	import com.kurst.utils.MovieUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import ru.inspirit.utils.JPGEncoder;
	
	public class ImageUtils	{
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-STATIC -----------------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------

		/**
		 * @method 
		 * @tooltip
		 * @param
		 * @return
		 */
		public static function resizeBitmap( originalBitmap : Bitmap , scaleFactor : Number ) : BitmapData{ 
			
			return MovieUtils.resizeBitmap( originalBitmap , scaleFactor );
			
		}
		/**
		 * @method 
		 * @tooltip
		 * @param
		 * @return
		 */
		public static function readPNGDim( f : File ) : Object {
			
			var ba		: ByteArray 	= new ByteArray();
			var stream	: FileStream 	= new FileStream();
			
			stream.open( f , FileMode.READ );
			stream.readBytes( ba );
			stream.close();
			
			var result	: Object		= ImageUtils.parsePNG( ba );
			
			ba.clear()
			stream 	= null;
			ba		= null;
			
			return result;
			
		}
		/**
		 * @method 
		 * @tooltip
		 * @param
		 * @return
		 */
		public static function readJPGDim( f : File ) : Object {

			var ba		: ByteArray 	= new ByteArray();
			var stream	: FileStream 	= new FileStream();
			
				stream.open( f , FileMode.READ);
				stream.readBytes( ba );
				stream.close();
			
			var result	: Object		= parseJPG( ba );
			
			ba.clear()
			stream 	= null;
			ba		= null;
			
			return result;
		}
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		//-STATIC - PRIVATE -----------------------------------------------------------------------------------------------------------------------------------------------------
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		
		/**
		 * @method 
		 * @tooltip
		 * @param
		 * @return
		 */
		private static function parsePNG(data: ByteArray ): Object {

			var start	: Number = data.readUnsignedByte();
			var result	: Object = new Object();
			
			if ( start == 137 ) {
				
				data.position 	= 16;		
				result.width 	= data.readUnsignedInt();
				result.height 	= data.readUnsignedInt();
				
				return result;
				
			} else { result = -1; }
			
			
			return -1;
			
		}
		/**
		 * @method 
		 * @tooltip
		 * @param
		 * @return
		 */
		private static function parseJPG(data: ByteArray ): Object {
			
			
			if (data.length < 4) return null;
			
			data.endian	 	= Endian.BIG_ENDIAN;
			data.position	= 0;
			
			var magic	: uint;
			var marker	: uint;
			var length	: uint;
			var w	 	: uint;
			var h	 	: uint;
			var result	: Object = new Object();
			
			magic	= data.readUnsignedShort();
			marker	= data.readUnsignedShort();
			
			if (magic != 0xFFD8) { 

				return -1;
			
			}
			
			while ( data.position < data.length - 1 ) {
				
				if ( marker == 0xFFC0 ) {
					
					data.position += 3;
					result.width = data.readUnsignedShort();
					result.height = data.readUnsignedShort();
					
					return result;
					
					
				} else {
					
					length			= data.readUnsignedShort() - 2;
					data.position	+= length;
					marker	 		= data.readUnsignedShort();
					
				}
				
			}
			
			return -1;
			
		}
		
		//------------------------------------------------------------------------------------------------------------------------------------------------------------
		
	}
	
}